package com.play.image;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.widget.ImageView;

public class GiftView extends ImageView {
	private Movie mMovie;
	private long mMovieStart;

	public GiftView(Context context) {
		super(context);
	}

	public GiftView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public GiftView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public void setSrcId(int srcId) {
		mMovie = Movie.decodeStream(getResources().openRawResource(srcId));
		this.invalidate();
	}

	public void setSrcPath(String path) {
		mMovie = Movie.decodeFile(path);
		this.invalidate();
	}
	
	public void onDraw(Canvas canvas) {
		long now = android.os.SystemClock.uptimeMillis();

		if (mMovieStart == 0) { // first time
			mMovieStart = now;
		}
		if (mMovie != null) {

			int dur = mMovie.duration();
			if (dur == 0) {
				dur = 1000;
			}
			int relTime = (int) ((now - mMovieStart) % dur);
			mMovie.setTime(relTime);
			mMovie.draw(canvas, this.getWidth() / 2 - mMovie.width() / 2,
					this.getHeight() / 2 - mMovie.height() / 2);
			invalidate();
		}
	}

}
